<%@ page language="java" import="java.util.*, com.zjut.zs.util.tools.*,com.zjut.zs.userinfo.manager.*, com.zjut.zs.userinfo.dao.*,
com.zjut.zs.user.manager.*, com.zjut.zs.user.dao.*,com.zjut.an.util.tools.*,com.zjut.an.util.system.*,
com.zjut.zs.school.manager.*, com.zjut.zs.school.dao.*, com.zjut.an.config.manager.*, com.zjut.an.config.dao.*" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
a {
	color: #00007f;
	text-decoration: underline;
}
-->
</style>
</head>

<body>
  	<!--顶部-->
	<div class="place">
    <span>位置：</span>
    <ul class="placeul">
    <li><a href="index.jsp">首页</a></li>
    <li><a href="#">考生注册</a></li>
    </ul>
    </div>
<%
AnConfigManager anconfig = (AnConfigManager) SpringContextUtil.getBean("AnConfigManager");
ZsSchoolManager zsManager = (ZsSchoolManager) SpringContextUtil.getBean("ZsSchoolManager");
ZsSchool school = new ZsSchool();
school = (ZsSchool)zsManager.getDataByInstance(1,1,null,null,school).getObjects().get(0);

//生成随机数x，传递给生成验证码的servlet:AuthenCodeImage,以防止出现外网访问验证码不变的情况
Random random=new Random();
double x=random.nextDouble();
%>
<%if(Status.ON.getStatus().equals(school.getScRegActive())){ //如果仍然可以登录%>
<form id="ff" name="add" role="form" class="form-horizontal" action="javascript:submitForm();" method="post"> 
    <div class="formbody">
    
    <div class="formtitle"><span>考生注册</span></div>
    
    <ul class="forminfo">
    <li><label>身份证号<b>*</b></label><input id="usIdcard" name="usIdcard" type="text" class="dfinput"  placeholder="身份证号" required data-rule-idCard="true" data-msg-required="不能为空" data-msg-idCard="请先输入考生身份证号" value=""  onblur="checkidcard()"/><i id="idcardlabel">第一步输入考生身份证号</i></li>
	<li><label>手机号<b>*</b></label>
    	<input id="usMobile" name="usMobile" type="text" class="dfinput" placeholder="高考手机号" required data-rule-mobile="true" data-msg-required="请输入手机号" data-msg-mobile="请输入正确格式" onkeyup="this.value=this.value.replace(/\D/g,'')" onblur="checkmobile()"  disabled/>
    	<i id="mobilelabel">第三步输入高考报名填写的手机号</i>
	</li>
    <li><label>验证码<b>*</b></label>
    	<input type="text" id="code" name="code" class="dfinput" onblur="checkcode()" required/>
			<button type="button" id="hqyzm" class="btn btn-primary btn-sm" onclick="getcode()">
				点击获取验证码
			</button><i id="codelabel">输入手机获取到的验证码</i>
    </li>
	<li><label>登录密码<b>*</b></label><input disabled="disabled" id="usPassword" name="usPassword" type="password" class="dfinput" placeholder="请设置登录密码" required data-rule-psw="true" data-msg-required="不能为空" data-msg-psw="8-20位的大写字母，小写字母，数字和特殊字符4种组合" minlength="8" maxlength="20" data-msg-minlength="请输入最小8位" /><i>8-20位的大写字母，小写字母，数字和特殊字符4种组合</i></li>
	<li><label>确认密码<b>*</b></label><input disabled="disabled" id="usPassword2" name="usPassword2" type="password" class="dfinput" placeholder="确认登录密码" required equalTo="#usPassword"/></li>
	    
    <li><label>&nbsp;</label>
    <input class="form-control" name="usId" type="text" id="usId" value="" style="display:none"/> 
    <button type="submit" class="btn btn-primary btn-sm">提交考生注册</button>  
        <a href="zs_login.jsp">登录</a>        <a href="getpassword1.jsp">找回密码</a>
    </li>
    <li id="message"></li>
    </ul>
    
    
    </div>
 </form>
<%}else{ %>
   <div style="margin:20px;">暂停注册！ <%out.print(anconfig.getSingleConfigInfoByName("暂停注册需知")); %></div> 
<%} %>

</body>
	
<script src="../plugins/assets/plugins/jquery-3.5.1.min.js" type="text/javascript"></script> 
<script src="../plugins/assets/plugins/jquery-validation-1.14.0/dist/jquery.validate.js" type="text/javascript"></script>
<script src="../plugins/assets/plugins/jquery-validation-1.14.0/dist/jquery.metadata.js" type="text/javascript"></script>
<script src="js/formvalidate.js" type="text/javascript"></script>
<script src="js/jquery.md5.js" type="text/javascript"></script>
	<script type="text/javascript">  
	$(function(){
	    //jquery.validate
		$("#ff").validate({
			submitHandler: function() {
					//验证通过后 的js代码写在这里
					submitForm();
			}
		})
	})
		function submitForm() {
			//$("#ff").validate({ 
				var jsonString = JSON.stringify(serializeToJSON('#ff'));
				//console.log(jsonString);
				ajaxSubmitForm(jsonString);
			//})			
		}

		function serializeToJSON(formname) {
			var serializeObj=serializeForm($(formname));  
			//console.info(serializeObj);
		    return serializeObj;   
		}

		function serializeForm(form) { /* 将form表单内的元素序列化为对象，扩展Jquery的一个方法 */
			var o = {};
			$.each(form.serializeArray(), function(index) {
				if (o[this['name']]) {
					o[this['name']] = o[this['name']] + "," + this['value'];
				} else {
					o[this['name']] = this['value'];
				}
			});
			return o;
		}

		var idcardcheck=0;
		var mobilecheck=0;
		var codecheck=0;
		var code;
		function checkidcard() {
			var idcard = $("#usIdcard").val();
			var label = document.getElementById("idcardlabel");
			$.ajax({
				async:false,
				cache:false,
				url: "../zsUserCount.action?usIdcard=" + idcard,
				type: "GET",
				dataType: "json",
			
				success: function(data) {
					console.info(data);
					if (data.total == "0") {
						$("#idcardlabel").css("color","red");
						$("#idcardlabel").html("仅限已报考本校三位一体的考生注册！报考学生名单中未找到该身份证，请确认身份证填写是否正确，考生是否已报考本校三位一体");
						idcardcheck=0;
						$('#usMobile').prop('disabled', true);
					} else {
						if (data.usactive == "已注册" ||data.usactive == "已注册") {
							$("#idcardlabel").css("color","red");
							$("#idcardlabel").html("您已经注册，请直接登录。如遗失密码请通过‘找回密码’重置");
							idcardcheck=0;
							$('#usMobile').prop('disabled', true);
						} else {
							$("#idcardlabel").css("color","green");
							$("#idcardlabel").html("身份证号码正确");
							idcardcheck=1;
							$('#usMobile').prop('disabled', false);
						}					
					}
				},
				error: function(xhr, type, errorThrown) {},
				complete: function(xhr, type, errorThrown) {}
			});
		}
	
		
	function checkmobile() {
		var mobile = $("#usMobile").val();
		var idcard = $("#usIdcard").val();
		var label = document.getElementById("mobilelabel");
		var phone = /^1\d{10}$/;
		if (phone.test(mobile)) {
			$.ajax({
				async:false,
				cache:false,
				url: "../zsApplyViewCountForReg.action?usIdcard=" + idcard+"&usMobile=" + mobile,
				type: "GET",
				dataType: "json",				
				success: function(data) {
					if (data.total == "0") {						
						$("#mobilelabel").css("color","red");
						$("#mobilelabel").html("该手机号与高考报名手机不符，请使用与高考报名记录一致的身份证号手机号");
						mobilecheck=0;
					} else {
						$("#mobilelabel").css("color","green");
						$("#mobilelabel").html("手机号码正确");
						mobilecheck=1;
					}
				},
				error: function(xhr, type, errorThrown) {},
				complete: function(xhr, type, errorThrown) {}
			});
		} else {
			$("#mobilelabel").css("color","red");
			if (mobile == "") $("#mobilelabel").html("请输入手机号码");
			else $("#mobilelabel").html("请输入正确的手机号码");
		}
	}
	
	function getcode() {
		//alert( idcardcheck+""+mobilecheck);
		if ((idcardcheck == 1) && (mobilecheck == 1)) {
			var mobile = $("#usMobile").val();
			//alert( $("#usMobile").val());
			$.ajax({
				async:false,
				cache:false,
				url: "../sendSMS.action?usMobile=" + mobile,
				type: "GET",
				dataType: "json",
				success: function(data) {
				    //alert(data);
					//code = data.code;
					var count = 60;
					
					if (data.flag == "success") {
						
						var yz = document.getElementById("hqyzm");
						yz.disabled = true;
						var timer = setInterval(function() {
							
							$("#hqyzm").html(count + "秒");
							count--;
							if (count == 0) {
								clearInterval(timer);
								$("#hqyzm").html("获取验证码");
								yz.disabled = false;
							}
						}, "1000");
					}
				},
				error: function(xhr, type, errorThrown) {},
				complete: function(xhr, type, errorThrown) {}
			});
		} else {
			checkmobile();
		}
	}

	function checkcode() {
		var temp = document.getElementById("code").value;
		var label = document.getElementById("codelabel");
		console.info(code);
		console.info(temp);
		if (temp == ""){
			$("#codelabel").css("color","red");
			$("#codelabel").html("请填写验证码");
			codecheck=0;
		} else {
			$.ajax({
				async:false,
				cache:false,
				url: "../checkCode.action?code=" + temp,
				type: "GET",
				dataType: "json",
			
				success: function(data) {
					if(data.message=="success"){
						$("#codelabel").css("color","green");
						$("#codelabel").html("验证码正确");
						codecheck=1;
						$("#usPassword").removeAttr("disabled");
						$("#usPassword2").removeAttr("disabled");
		        	} else {
		        		$("#codelabel").css("color","red");
		    			$("#codelabel").html("验证码错误");
		    			codecheck=0;
		        	}
				},
				error: function(xhr, type, errorThrown) {},
				complete: function(xhr, type, errorThrown) {}
			});
		}
	}
	
		
	function ajaxSubmitForm(jsonString) {
		//alert(codecheck);
		if (idcardcheck==1 && mobilecheck == 1 && codecheck == 1) {
			//验证通过后 的js代码写在这里					
			console.log(jsonString);
			$.ajax({
		    	cache: false,
		    	type: "POST",
		        url:"../sendPassword.action",
		        data:{rowstr:jsonString},// 你的formid
		        async: false,
		        error: function(request) {
		        	alert("网络有误");
		        },
		        success: function(data) {
		        	console.log(data);
		        	if(data.message=="success"){
		            	alert("注册成功！");
		            	top.window.location = 'main.jsp';
		        	} else {
		        		alert("注册失败！");
		        		window.location = 'zs_reg.jsp';
		        	}
		        }
		    });
		} else {
			alert("请先输入高考报名时填写的身份证、手机号");
		}			
	}
		
	</script>
</html>